const Koa = require('koa')
const cors = require('koa2-cors') // 解决跨域问题
// const bodyParser = require('koa-bodyparser')
const { koaBody } = require('koa-body') // 解析文件上传的请求
const static = require('koa-static') // 处理静态资源
const messageRouter = require('./router/message')
const userRouter = require('./router/user')

const app = new Koa()
// koa-bodyparser中间件解析post请求的body为一个JSON对象，并挂载到ctx.request.body属性上
// app.use(bodyParser())
app.use(
  koaBody({
    multipart: true,
    formidable: {
      uploadDir: './public/uploads',
      keepExtensions: true
    }
  })
) // 解析请求体中的formData
app.use(cors()) // 设置允许跨域
app.use(static('./public')) // 将 public 目录设置为静态资源目录
app.use(messageRouter.routes(), messageRouter.allowedMethods())
app.use(userRouter.routes(), userRouter.allowedMethods())

app.listen(3000, () => {
  console.log(`Server is running on http://localhost:3000`)
})
